package com.xxx.baseapplication

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.xxx.baseapplication.dao.AppDataBase
import com.xxx.baseapplication.databinding.ActivityMain2Binding
import com.xxx.baseapplication.fragment.ProjectFragment
import com.xxx.baseapplication.vo.TabItem
import com.xxx.baseapplication.vo.UrlTypeVO
import com.xxx.mylibrary.base.BaseActivity
import kotlinx.android.synthetic.main.view_tab.view.*

class TestActivity : BaseActivity<ActivityMain2Binding>() {

    override val layoutId: Int
        get() = R.layout.activity_main2

    private val tabs = arrayOf(
        TabItem(
            R.mipmap.ic_tab_home_s,
            "首页",
            HomeFragment::class.java
        ),
        TabItem(
            R.mipmap.ic_tab_home_s,
            "项目",
            ProjectFragment::class.java
        )
    )

    private val fragments = ArrayList<Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initFragments()
        initTabLayout()

    }

    private fun initTabLayout() {
        binding.tabLayout.run {
            tabs.forEach {
                addTab(newTab().setCustomView(getTabView(it)))
            }

            addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{

                override fun onTabReselected(p0: TabLayout.Tab?) {}

                override fun onTabUnselected(p0: TabLayout.Tab?) {}

                override fun onTabSelected(p0: TabLayout.Tab) {
                    initTab(p0.position)
                }
            })

            getTabAt(0)?.select()
        }

        initTab(0)
    }

    private fun initTab(position: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        fragments.forEachIndexed{ index, fragment ->
            if (index == position) {
                transaction.show(fragment)
            } else {
                transaction.hide(fragment)
            }
        }
        transaction.commit()
    }


    private fun getTabView(it: TabItem): View {
        val v = LayoutInflater.from(this).inflate(R.layout.view_tab, null)
        v.run {
            tab_icon.setImageResource(it.icon)
            tab_name.text = it.name
            return this
        }
    }

    private fun initFragments() {

        AppDataBase.get().urlTypeDao().insert(UrlTypeVO("1","1"))
        AppDataBase.get().urlTypeDao().insert(UrlTypeVO("2","2"))
        if (fragments.isEmpty()) {
            tabs.forEach {
                val f = it.fragmentCls.newInstance()
                fragments.add(f)
            }
        }

        val transaction = supportFragmentManager.beginTransaction()
        fragments.forEach{
            if (!it.isAdded) transaction.add(R.id.fl_content, it, it.javaClass.simpleName).hide(it)
        }
        transaction.commit()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        fragments.forEach {
            supportFragmentManager.putFragment(
                outState,
                it.javaClass.simpleName,
                it
            )
        }
        super.onSaveInstanceState(outState)
    }
}